package com.timbacsi.main;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.easycarebn.main.fragment.FragmentAppointmentDetailt;
import com.easycarebn.main.fragment.FragmentBook;
import com.easycarebn.main.fragment.FragmentDrDetailt;
import com.easycarebn.main.fragment.FragmentForgetPass;
import com.easycarebn.main.fragment.FragmentHome;
import com.easycarebn.main.fragment.FragmentLogin;
import com.easycarebn.main.fragment.FragmentMapContact;
import com.easycarebn.main.fragment.FragmentMapDetailt;
import com.easycarebn.main.fragment.FragmentMessageDetailt;
import com.easycarebn.main.fragment.FragmentPromotions;
import com.easycarebn.main.fragment.FragmentRegister;
import com.easycarebn.main.fragment.FragmentResultMap;
import com.easycarebn.main.utils.IDataResponse;
import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.data.UserResponse;

public class DetailtActivity extends SherlockFragmentActivity implements
		IResult {

	private int keyOption = Integer.MAX_VALUE;
	public static ProgressBar loadding;
	private IDataResponse mDataResponse;
	private EasyCareApplication mEaApplication;
	public static Activity activity = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = DetailtActivity.this;
		mEaApplication = (EasyCareApplication) getApplicationContext();
		overridePendingTransition(R.anim.anim_slide_in_left,
				R.anim.anim_slide_out_left);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.activity_detailt);

		loadding = (ProgressBar) findViewById(R.id.loadding);
		loadding.setVisibility(View.GONE);

		changeTaskbar(R.drawable.ic_back, R.drawable.bgr_tasktop_green,
				R.layout.layout_taskbar_children);

		Intent intent = getIntent();
		int key = 0;
		if (intent != null) {
			key = intent.getIntExtra(Utils.KEY_OPTION, 0);
		}
		switch (key) {
		case Utils.MAP_ALL_SEARCH_RESULT:
			addFragment(FragmentResultMap.getInstance(), Utils.TAG_CHILD);
			break;

		case Utils.NOTIFICATION_DETAILT:

			addFragment(FragmentMessageDetailt.newInstance(getResources()
					.getString(R.string.chitiet_tinnhan), getIntent()
					.getExtras()), Utils.TAG_CHILD);
			break;

		case Utils.MAP_CONTACT:
			showMapView(intent.getExtras(), Utils.TAG_CHILD);
			break;

		case Utils.MAP_CONTACT_DETAILT:
			addFragment(FragmentMapDetailt.getInstance(), Utils.TAG_CHILD);
			break;

		case Utils.DOCTOR_SERVICE_DETAILT:
			keyOption = Utils.MAP_DOCTER;

			addFragment(FragmentDrDetailt.newInstance(""), Utils.TAG_CHILD);

			break;
		case Utils.DOCTOR_SERVICE_DETAILT_PK:
			keyOption = Utils.MAP_DOCTER;
			addFragment(new FragmentDrDetailt(), Utils.TAG_CHILD);

			break;
		case Utils.APPOINTMENT_DETAIL:
			addFragment(FragmentAppointmentDetailt.newInstance(getResources()
					.getString(R.string.quan_li_hen_kham), intent.getExtras()),
					Utils.TAG_CHILD);

			break;
		case Utils.LOGIN_CMT:
			addFragment(
					FragmentLogin.newInstance(getResources().getString(
							R.string.lbldatlichkham)), Utils.TAG_CHILD);

			break;
		case Utils.LOGIN_BOOKING:
			addFragment(
					FragmentLogin.newInstance(getResources().getString(
							R.string.lbldatlichkham)), Utils.TAG_CHILD);

			break;

		case Utils.BOOKING:
			addFragment(FragmentBook.newInstance(intent.getExtras()),
					Utils.TAG_CHILD);

			break;
		case Utils.MESSAGES:

			String mType = intent.getExtras().getString(Utils.KEY_TYPE);

			/*
			 * if (mType.equals(GcmIntentService.type_appointment)) {
			 * addFragment(
			 * FragmentAppointmentDetailt.newInstance(getResources()
			 * .getString(R.string.quan_li_hen_kham), intent.getExtras()),
			 * Utils.TAG_CHILD); } else
			 */

			if (mType.equals(GcmIntentService.type_promotion)) {
				addFragment(FragmentPromotions.newInstance(intent.getExtras()),
						Utils.TAG_CHILD);
			} else {
				addFragment(FragmentMessageDetailt.newInstance(getResources()
						.getString(R.string.chitiet_tinnhan), getIntent()
						.getExtras()), Utils.TAG_CHILD);

			}

			break;

		}
	}

	public void addFragment(Fragment fragment, String tag) {
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.fragment_content_detailt, fragment, tag)
				.addToBackStack(null).commit();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBack();

			break;
		case Utils.MAP_DOCTER:
			if (FragmentDrDetailt.newInstance("").getListAddresses() != null
					&& FragmentDrDetailt.newInstance("").getListAddresses()
							.size() > 0)
				Utils.intentToDetailt(DetailtActivity.this,
						Utils.MAP_CONTACT_DETAILT);

			break;
		}

		return super.onOptionsItemSelected(item);
	}

	public void showMapView(Bundle data, String tag) {
		getSupportFragmentManager()
				.beginTransaction()
				.add(R.id.fragment_content_detailt,
						FragmentMapContact.getInstance(data), tag)
				.addToBackStack(null).commit();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_CANCELED) {
			return;
		}
		if (requestCode == Utils.LOGIN_CMT) {
			mEaApplication.setLogin(true);
			return;
		}

		if (requestCode == Utils.LOGIN_BOOKING) {

			UserResponse userResponse = Utils
					.getUserLogin(DetailtActivity.this);
			if (userResponse != null) {
				Utils.showMessage(DetailtActivity.this, getResources()
						.getString(R.string.dang_nhap_thanh_cong));
				mEaApplication.setLogin(true);
				FragmentDrDetailt.newInstance("").update();
				Utils.gotoBooking(DetailtActivity.this, FragmentDrDetailt
						.newInstance("").getDataBooking(), FragmentHome
						.newInstance().getSelectedSearchType());

			}
			return;
		}
		if (requestCode == Utils.REQ_ATTACH_FILE) {
			if (mDataResponse != null) {
				mDataResponse.onResult(requestCode, data);
			}
			return;

		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (keyOption == Utils.MAP_DOCTER) {
			menu.add(0, Utils.MAP_DOCTER, Menu.NONE, "Map")
					.setIcon(R.drawable.ic_location_white)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		} else {
			menu.add(0, Integer.MAX_VALUE, Menu.NONE, "Add")
					.setIcon(android.R.color.transparent)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onBackPressed() {
		onBack();
	}

	public boolean isPageChild() {
		Fragment fragment = getSupportFragmentManager().findFragmentByTag(
				Utils.TAG_CHILD);
		if (fragment == null)
			return false;
		return fragment.isVisible();
	}

	public void onBack() {

		Intent intent = new Intent();
		intent.putExtra(Utils.KEY_OPTION, Integer.MAX_VALUE);
		setResult(Integer.MAX_VALUE, intent);
		finish();

		overridePendingTransition(R.anim.anim_slide_in_right,
				R.anim.anim_slide_out_right);
	}

	public void changeTaskbar(int icon, int bgr, int taskbar) {
		getSupportActionBar().setDisplayOptions(
				ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
		getSupportActionBar().setCustomView(taskbar);
		getSupportActionBar().setIcon(getResources().getDrawable(icon));
		getSupportActionBar().setBackgroundDrawable(
				getResources().getDrawable(bgr));
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(true);

	}

	@Override
	public void onDisplay(int arg1, String arg2) {
		switch (arg1) {
		case Utils.SHOW_HOME:
			Intent intent = new Intent();
			intent.putExtra(Utils.KEY_OPTION,
					getIntent().getIntExtra(Utils.KEY_OPTION, 0));
			setResult(getIntent().getIntExtra(Utils.KEY_OPTION, 0), intent);
			finish();
			overridePendingTransition(R.anim.anim_slide_in_right,
					R.anim.anim_slide_out_right);

			break;
		case Utils.FORGET_PASS:
			addFragment(FragmentForgetPass.newInstance(arg2), Utils.TAG_CHILD);

			break;
		case Utils.CREATE_ACC:
			String array[] = arg2.split("-");
			Bundle bundle = new Bundle();
			bundle.putString(Utils.KEY_TITLE, array[0]);
			bundle.putString(Utils.KEY_PHONE, array[1]);

			addFragment(FragmentRegister.newInstance(bundle), Utils.TAG_CHILD);

			break;
		case Utils.ONBACK:
			onBack();
			break;

		case Utils.LOGIN:
			addFragment(
					FragmentLogin.newInstance(getResources().getString(
							R.string.lbldatlichkham)), Utils.TAG_CHILD);

			break;

		}
	}

	public void setDataResponse(IDataResponse listener) {
		mDataResponse = listener;
	}

}
